package br.project.cdd.dao;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import br.project.cdd.bean.CddBean;;

public class CddDAO extends Conexao{
	
	public CddDAO() 
	{
		super();		
	}

	public void inserirCdd(CddBean cdd)
	{
		try
		{
			String sql = "INSERT INTO CDD (NOME_CDD, LOCALIDADE, UF, NUM_CDD) VALUES ('" + cdd.getNome_cdd() + "', '" + cdd.getLocalidade() + "', '" + cdd.getUf() + "', '" + cdd.getNum_cdd() + "')";
			Statement stmt = conexao.createStatement();
			stmt.executeUpdate(sql);
		}
		catch(SQLException sqlEx)
		{
			sqlEx.printStackTrace();
		}
	}
	
	public ArrayList<CddBean> selecionarTodosCdds()
	{
		ArrayList<CddBean> listCdds = new ArrayList<CddBean>();
		CddBean cdds = null;
		try
		{
			String sql = "SELECT ID, NOME_CDD, LOCALIDADE, UF, NUM_CDD FROM CDD";
			Statement stmt = conexao.createStatement();
			ResultSet consulta = stmt.executeQuery(sql);
			
			while (consulta.next())
			{
				cdds = new CddBean();				
				cdds.setId(consulta.getInt(1));
				cdds.setNome_cdd(consulta.getString(2));
				cdds.setLocalidade(consulta.getString(3));
				cdds.setUf(consulta.getString(4));
				cdds.setNum_cdd(consulta.getString(5));
				
				listCdds.add(cdds);
			}
		}
		catch(SQLException sqlEx)
		{
			sqlEx.printStackTrace();
		}
		
		return listCdds;
	}
	
	public int buscarIdCdd(String num_cdd){
		int idCdd = -1;
		
		try{
			String sql = "SELECT ID FROM CDD WHERE NUM_CDD = '" + num_cdd + "'";
			Statement stmt = conexao.createStatement();
			ResultSet consulta = stmt.executeQuery(sql);
			
			while(consulta.next()){
				idCdd = consulta.getInt(1);
			}
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
		
		return idCdd;
	}

}
